package com.umer.pinban.user;

import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.widget.SwitchCompat;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.UserInfo;
import com.common.db.UserDao;
import com.common.okhttp.OkSimpleRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.utils.AntiShake;
import com.common.utils.ApkUtils;
import com.common.utils.Constants;
import com.common.utils.GlideUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 用户
 */
public class UserActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.my_image_view)
    RoundedImageView myImageView;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_shop_phone)
    TextView tvShopPhone;
    @BindView(R.id.tv_shop_address)
    TextView tvShopAddress;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.linear_1)
    LinearLayout linear1;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.linear_2)
    LinearLayout linear2;
    @BindView(R.id.tv_3)
    TextView tv3;
    @BindView(R.id.linear_3)
    LinearLayout linear3;
    @BindView(R.id.tv_4)
    TextView tv4;
    @BindView(R.id.linear_4)
    LinearLayout linear4;
    @BindView(R.id.tv_5)
    TextView tv5;
    @BindView(R.id.linear_5)
    LinearLayout linear5;
    @BindView(R.id.tv_6)
    TextView tv6;
    @BindView(R.id.linear_6)
    LinearLayout linear6;
    @BindView(R.id.tv_7)
    TextView tv7;
    @BindView(R.id.linear_7)
    LinearLayout linear7;
    @BindView(R.id.switchCompat)
    SwitchCompat switchCompat;
    private UserInfo userInfo;
    private int hidePhoneStatus = 0;//隐藏顾客手机状态
    private UserDao userDao;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_user_center;
    }

    @Override
    public void init() {
        userDao = new UserDao(this);
        userInfo = AppHolder.getInstance().getUserInfo();
        if (userInfo != null) {
            GlideUtils.getInstance().loadHeadImageView(this,
                    Constants.IP_PORT_DEFAULT_PICTURE + userInfo.getHeader(), myImageView);
            if (userInfo.getName() != null && !userInfo.getName().equals("null")
                    && !userInfo.getName().equals(" ") && !userInfo.getName().equals("")) {
                tvShopName.setText(userInfo.getName());
            } else {
                tvShopName.setText("");
            }
            if (userInfo.getPhone() != null && !userInfo.getPhone().equals("null")
                    && !userInfo.getPhone().equals(" ") && !userInfo.getPhone().equals("")) {
                tvShopPhone.setText(userInfo.getPhone());
            } else {
                tvShopPhone.setText("");
            }
            if (userInfo.getAddress() != null && !userInfo.getAddress().equals("null")
                    && !userInfo.getAddress().equals(" ") && !userInfo.getAddress().equals("")) {
                tvShopAddress.setText(userInfo.getAddress());
            } else {
                tvShopAddress.setText("");
            }
            //到期
            if (userInfo.getMemberTime() != null && !userInfo.getMemberTime().equals("null")
                    && !userInfo.getMemberTime().equals(" ") && !userInfo.getMemberTime().equals("")) {
                tv1.setText(userInfo.getMemberTime() + "到期");
            } else {
                tv1.setText("续费");
            }
            hidePhoneStatus = userInfo.getHidePhoneStatus();
            if (hidePhoneStatus == 0){
                switchCompat.setChecked(true);
                switchCompat.setText("已打开");
                tv2.setTextColor(getColor(R.color.black));
                switchCompat.setTextColor(getColor(R.color.black));
                tv2.setText("员工可以正常查看客户手机号码和拨打联系客户");
            }else {
                switchCompat.setChecked(false);
                switchCompat.setText("已关闭");
                tv2.setText("员工无法正常查看客户手机号码和拨打联系客户");
                tv2.setTextColor(getColor(R.color.colorPrimary));
                switchCompat.setTextColor(getColor(R.color.colorPrimary));
            }
        }
        tv5.setText(ApkUtils.getInstance().getVersionName());
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    open(1);
                }else {
                    open(0);
                }
            }
        });
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back, R.id.my_image_view, R.id.linear_6,R.id.linear_1,R.id.linear_3,R.id.linear_7, R.id.linear_4})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                finish();
                break;
            //修改密码
            case R.id.linear_4:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                switchTo(this,UpDateUserPassWordActivity.class,false);
                break;
            //修改信息
            case R.id.linear_3:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                switchTo(this,UpDateUserActivity.class,false);
                break;
            //帮助加微信
            case R.id.linear_6:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                switchTo(this,UmerHelpActivity.class,false);
                break;
            case R.id.my_image_view:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                break;
            case R.id.linear_1:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                switchTo(UserActivity.this, XuFeiActivity.class, false);
                break;
            //退出登录
            case R.id.linear_7:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                new MaterialDialog.Builder(this)
                        .title("确认退出")
                        /*.content("包含输入框的diaolog")
                                .widgetColor(Color.BLUE)//输入框光标的颜色*/
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(MaterialDialog dialog, DialogAction which) {
                                //userDao.deleteAll();
                                Intent i = new Intent(UserActivity.this, UserLoginActivity.class);
                                i.putExtra("flag", 2);
                                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i);
                                finish();
                            }
                        })
                        .positiveText("确定")
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(MaterialDialog dialog,DialogAction which) {
                            }
                        })
                        .negativeText("取消")
                        .backgroundColor(getColor(R.color.white))
                        .itemsColor(getColor(R.color.black))
                        .negativeColor(getColor(R.color.colorPrimary))
                        .positiveColor(getColor(R.color.colorPrimary))
                        .titleColor(getColor(R.color.black))
                        .show();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what){
            case R.id.updateShopHidePhoneStatus:
                hideProgress();
                if(hidePhoneStatus == 0){
                    hidePhoneStatus = 1;
                    AppHolder.getInstance().getUserInfo().setHidePhoneStatus(1);
                    userDao.update( AppHolder.getInstance().getUserInfo());
                }else {
                    hidePhoneStatus = 0;
                    AppHolder.getInstance().getUserInfo().setHidePhoneStatus(0);
                    userDao.update( AppHolder.getInstance().getUserInfo());
                }
                if (hidePhoneStatus == 0){
                    switchCompat.setChecked(true);
                    switchCompat.setText("已打开");
                    tv2.setTextColor(getColor(R.color.black));
                    switchCompat.setTextColor(getColor(R.color.black));
                    tv2.setText("员工可以正常查看客户手机号码和拨打联系客户");
                }else {
                    switchCompat.setChecked(false);
                    switchCompat.setText("已关闭");
                    tv2.setText("员工无法正常查看客户手机号码和拨打联系客户");
                    tv2.setTextColor(getColor(R.color.colorPrimary));
                    switchCompat.setTextColor(getColor(R.color.colorPrimary));
                }
                break;
            default:break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.updateShopHidePhoneStatus:
                hideProgress();
                t.getDesc();
                if (hidePhoneStatus == 0){
                    switchCompat.setChecked(true);
                    switchCompat.setText("已打开");
                    tv2.setTextColor(getColor(R.color.black));
                    switchCompat.setTextColor(getColor(R.color.black));
                    tv2.setText("员工可以正常查看客户手机号码和拨打联系客户");
                }else {
                    switchCompat.setChecked(false);
                    switchCompat.setText("已关闭");
                    tv2.setText("员工无法正常查看客户手机号码和拨打联系客户");
                    tv2.setTextColor(getColor(R.color.colorPrimary));
                    switchCompat.setTextColor(getColor(R.color.colorPrimary));
                }
                break;
            default:break;
        }
    }

    /**
     */
    private void open(int hidePhoneStatus){
        if(hidePhoneStatus == 0){
            showProgress("正在关闭,请稍后...");
        }else {
            showProgress("正在打开,请稍后...");
        }
        OkSimpleRequest okSimpleRequest = new OkSimpleRequest(R.id.updateShopHidePhoneStatus,
                Constants.updateShopHidePhoneStatus);
        okSimpleRequest.addParams("shopId", userInfo.getShopId());
        okSimpleRequest.addParams("hidePhoneStatus", hidePhoneStatus);
        okSimpleRequest.setHeader(true);
        requestOkhttpSimple(okSimpleRequest);
    }
}
